package com.hbnu.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo2 {
    public static void main(String[] args) {
        try {
            //创建Socket端口，并监听端口
            ServerSocket serverSocket = new ServerSocket(8888);

            //获取客户端Socket
            Socket socket = serverSocket.accept();

            //获取流资源
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();

            //获取客户端发送的数据
            byte[] bytes = new byte[1024];
//            int temp = 0;
//            int length = 0;
//            while ((temp = in.read())!=-1) {
//                bytes[length++] = (byte) temp;
//            }                                             //这一段诈胡了，好像死循环了
            int length = in.read(bytes);

            //输出客户端发送的数据
            System.out.println(new String(bytes,0,length));

            //向客户端返回数据
            String content = "1111111111100 1011011";
            out.write(content.getBytes());

            //关闭资源
            in.close();
            socket.close();
            serverSocket.close();


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
